package com.buddies.lottery.utils;

import com.buddies.lottery.common.ConfigManager;
import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.tencent.bugly.crashreport.BuglyLog;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class AlarmUtils {

    private static final String TAG = AlarmUtils.class.getSimpleName();
    private static final String PUSH_BEAR = "https://pushbear.ftqq.com/sub?sendkey=%s&text=%s&desp=%s";
    private static final String SEND_KEY = "6390-d01907c3a28589e4ddb95d60a780503";

    private static AlarmUtils mInstance;

    private OkHttpClient mHttpClient = null;

    private AlarmUtils() {
    }

    public static synchronized AlarmUtils getInstance() {
        if (mInstance == null) {
            mInstance = new AlarmUtils();
        }
        return mInstance;
    }

    public void send(String text, String desp) {
        if (mHttpClient == null) {
            mHttpClient = new OkHttpClient.Builder()
                    .addNetworkInterceptor(new StethoInterceptor())
                    .writeTimeout(5, TimeUnit.SECONDS)
                    .build();
        }

        desp = "登录账号@" + ConfigManager.getInstance().getUserId() + "@" + desp + "@" + AppUtils.getAppVersionInfo();

        String url = String.format(PUSH_BEAR, SEND_KEY, text, desp);
        Call call = mHttpClient.newCall(new Request.Builder().url(url).get().build());
        call.enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                BuglyLog.e(TAG,  "onFailure", e);
            }

            @Override
            public void onResponse(Call call, Response response) {
                BuglyLog.d(TAG, "onResponse:" + response.message());
            }
        });
    }

}
